package k5_net.demo01;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TCPClient {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        try {
            Socket socket = new Socket("localhost",8888);
            System.out.println("客户端已经成功连接服务端");

            //把客户端的内容封装成字符打印输出流
            PrintWriter pw = new PrintWriter(socket.getOutputStream());

            System.out.println("开始聊天了");

            while(true){
                //从控制台读取信息
                String message = scanner.nextLine();
                pw.println(message);
                pw.flush();
                if(message.equals("拜拜")){
                    break;
                }
            }

            System.out.println("聊天结束");
            pw.close();
            socket.close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
